package com.qingzhu.dao.impl;

import com.qingzhu.dao.ItemDao;
import com.qingzhu.pojo.Item;
import com.qingzhu.util.CreateSqlSessionFactory;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ItemDaoImpl implements ItemDao {

    @Override
    public Item findById(int itemId, boolean excludeDel) {
        try (SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)) {
            ItemDao itemDao = sqlSession.getMapper(ItemDao.class);
            return itemDao.findById(itemId,excludeDel);
        }
    }

    @Override
    public List<Item> findByGoodsAll(int goodsId, boolean excludeDel) {
        try (SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)) {
            ItemDao itemDao = sqlSession.getMapper(ItemDao.class);
            return itemDao.findByGoodsAll(goodsId,excludeDel);
        }
    }

    @Override
    public Item findItem(int goodsId, String edition, String colour, String network) {
        try (SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)) {
            ItemDao itemDao = sqlSession.getMapper(ItemDao.class);
            return itemDao.findItem(goodsId, edition, colour, network);
        }
    }

    @Override
    public List<Integer> findItemFromSpec(String colour, String network, String runMemory, String memory) {
        try (SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)) {
            ItemDao itemDao = sqlSession.getMapper(ItemDao.class);
            return itemDao.findItemFromSpec(colour,network,runMemory,memory);
        }
    }

    @Override
    public int addItem(Item item) {
        try (SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)) {
            ItemDao itemDao = sqlSession.getMapper(ItemDao.class);
            return itemDao.addItem(item);
        }
    }

    @Override
    public int updateItem(Item item) {
        try (SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)) {
            ItemDao itemDao = sqlSession.getMapper(ItemDao.class);
            return itemDao.updateItem(item);
        }
    }

    @Override
    public int updateItemState(String state, String itemId) {
        try (SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)) {
            ItemDao itemDao = sqlSession.getMapper(ItemDao.class);
            return itemDao.updateItemState(state, itemId);
        }
    }

    @Override
    public int downItemByGoodsId(String state, String goodsId) {
        try (SqlSession sqlSession = CreateSqlSessionFactory.getFactory().openSession(true)) {
            ItemDao itemDao = sqlSession.getMapper(ItemDao.class);
            return itemDao.downItemByGoodsId(state,goodsId);
        }
    }
}
